-
Notifications
You must be signed in to change notification settings - Fork 277
use code_blockt::add and ::move for adding a statement #3067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -2744,7 +2744,7 @@ codet java_bytecode_convert_methodt::convert_iinc( | |||
arg1_int_type.make_typecast(java_int_type()); | |||
code_assign.rhs() = | |||
plus_exprt(variable(arg0, 'i', address, CAST_AS_NEEDED), arg1_int_type); | |||
block.copy_to_operands(code_assign); | |||
block.add(code_assign); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -215,13 +215,13 @@ exprt allocate_dynamic_object( | |||
symbols_created.push_back(&malloc_sym); | |||
code_assignt assign=code_assignt(malloc_sym.symbol_expr(), malloc_expr); | |||
assign.add_source_location()=loc; | |||
output_code.copy_to_operands(assign); | |||
output_code.add(assign); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
exprt malloc_symbol_expr=malloc_sym.symbol_expr(); | ||
if(cast_needed) | ||
malloc_symbol_expr=typecast_exprt(malloc_symbol_expr, target_expr.type()); | ||
code_assignt code(target_expr, malloc_symbol_expr); | ||
code.add_source_location()=loc; | ||
output_code.copy_to_operands(code); | ||
output_code.add(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -230,7 +230,7 @@ exprt allocate_dynamic_object( | |||
null_pointer_exprt null_pointer_expr(to_pointer_type(target_expr.type())); | |||
code_assignt code(target_expr, null_pointer_expr); | |||
code.add_source_location()=loc; | |||
output_code.copy_to_operands(code); | |||
output_code.add(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -320,7 +320,7 @@ exprt java_object_factoryt::allocate_object( | |||
aoe=typecast_exprt(aoe, target_expr.type()); | |||
code_assignt code(target_expr, aoe); | |||
code.add_source_location()=loc; | |||
assignments.copy_to_operands(code); | |||
assignments.add(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -1237,7 +1235,7 @@ void java_object_factoryt::gen_nondet_init( | |||
code_assignt assign(expr, rhs); | |||
assign.add_source_location()=loc; | |||
|
|||
assignments.copy_to_operands(assign); | |||
assignments.add(assign); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -1408,13 +1406,13 @@ void java_object_factoryt::gen_nondet_array_init( | |||
exprt counter_expr=counter.symbol_expr(); | |||
|
|||
exprt java_zero=from_integer(0, java_int_type()); | |||
assignments.copy_to_operands(code_assignt(counter_expr, java_zero)); | |||
assignments.add(code_assignt(counter_expr, java_zero)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -1393,7 +1391,7 @@ void java_object_factoryt::gen_nondet_array_init( | |||
const auto &array_init_symexpr=array_init_symbol.symbol_expr(); | |||
codet data_assign=code_assignt(array_init_symexpr, init_array_expr); | |||
data_assign.add_source_location()=loc; | |||
assignments.copy_to_operands(data_assign); | |||
assignments.add(data_assign); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -1328,7 +1326,7 @@ void java_object_factoryt::allocate_nondet_length_array( | |||
java_new_array.type().subtype().set(ID_element_type, element_type); | |||
code_assignt assign(lhs, java_new_array); | |||
assign.add_source_location() = loc; | |||
assignments.copy_to_operands(assign); | |||
assignments.add(assign); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
@@ -170,7 +170,7 @@ void java_simple_method_stubst::create_method_stub(symbolt &symbol) | |||
code_assignt get_argument( | |||
init_symbol_expression, symbol_exprt(this_argument.get_identifier())); | |||
get_argument.add_source_location() = synthesized_source_location; | |||
new_instructions.copy_to_operands(get_argument); | |||
new_instructions.add(get_argument); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 29bd494).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86391063
@@ -141,7 +141,7 @@ static void monitor_exits(codet &code, const codet &monitorexit) | |||
// Replace the return with a monitor exit plus return block | |||
code_blockt return_block; | |||
return_block.add(monitorexit); | |||
return_block.move_to_operands(code); | |||
return_block.move(code); | |||
code = return_block; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about code = code_blockt({monitorexit, code});
try_block.move(catch_push); | ||
try_block.move(code); | ||
try_block.move(catch_pop); | ||
try_block.move(catch_label); | ||
code = try_block; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code = code_blockt({monitorenter, catch_push, code, catch_pop, catch_label});
@@ -856,7 +856,7 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table) | |||
code_blockt clone_body; | |||
|
|||
code_declt declare_cloned(local_symexpr); | |||
clone_body.move_to_operands(declare_cloned); | |||
clone_body.move(declare_cloned); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the declaration of clone_body
further down and do code_blockt clone_body({declare_cloned, create_blank, declare_index, copy_loop, return_inst});
(if I got this right from what I see in the code review).
ret_block.move_to_operands(clinit_call); | ||
ret_block.move_to_operands(c); | ||
ret_block.move(clinit_call); | ||
ret_block.move(c); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt ret_block({clinit_call, c});
ret_block.move_to_operands(assert_code); | ||
ret_block.move_to_operands(assume_code); | ||
ret_block.move(assert_code); | ||
ret_block.move(assume_code); | ||
c = ret_block; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt ret_block({assert_code, assume_code});
(and other places use std::move
afterwards).
src/cpp/cpp_typecheck_code.cpp
Outdated
@@ -163,7 +163,8 @@ void cpp_typecheckt::typecheck_switch(code_switcht &code) | |||
c_typecheck_baset::typecheck_switch(code); | |||
|
|||
code_blockt code_block; | |||
code_block.move_to_operands(decl.op0(), code); | |||
code_block.move(to_code(decl.op0())); | |||
code_block.move(code); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt code_block({to_code(decl.op0()), code});
@@ -1748,7 +1748,7 @@ void goto_program2codet::cleanup_code_ifthenelse( | |||
// we re-introduce 1-code blocks with if-then-else to avoid dangling-else | |||
// ambiguity | |||
code_blockt b; | |||
b.move_to_operands(i_t_e.then_case()); | |||
b.move(i_t_e.then_case()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt b({i_t_e.then_case()});
@@ -1760,7 +1760,7 @@ void goto_program2codet::cleanup_code_ifthenelse( | |||
// we re-introduce 1-code blocks with if-then-else to avoid dangling-else | |||
// ambiguity | |||
code_blockt b; | |||
b.move_to_operands(i_t_e.else_case()); | |||
b.move(i_t_e.else_case()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt b({i_t_e.else_case()});
b.move_to_operands(else_label); | ||
b.move(i_t_e); | ||
b.move(then_label); | ||
b.move(else_label); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt b({i_t_e, then_label, else_label});
src/util/std_code.cpp
Outdated
result.copy_to_operands(code_assertt(condition)); | ||
result.copy_to_operands(code_assumet(condition)); | ||
result.add(code_assertt(condition)); | ||
result.add(code_assumet(condition)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code_blockt result({code_assertt(condition), code_assumet(condition)});
@tautschnig @peterschrammel Please note that your suggestions to into opposite directions: Michael's means more copying, Peter wants less. |
Well, yes, but on different bits of the code base. @peterschrammel's suggestions exclusively cover cases that require (at least so it seems) incremental construction; mine talks about cases where likely we could even add a |
Perhaps to make more fundamental progress on this:
with move_to_operands: 0.59s This does not appear to require a recent compiler. The difference is actually difficult to measure (it's in the range of the noise). Thus, there is absolutely no reason to believe that moving ireps would be any faster than copying. The key reason why the moving methods exist is that they did offer enormous benefit before the sharing was introduced. I am thus tempted to remove the .move() method, say for code_blockt as a starter. It's simply a distraction. |
@kroening Thank you for doing this measurement! Radical as I am, may I propose the even bigger step of getting rid of |
29bd494
to
0a16bb6
Compare
Done; please take another look. |
@tautschnig Indeed, will do a PR that deprecates move_to_operands(). |
Hang on, the move operations is surely only similar like this when there is no usage of the moved
This will cause a CoW break upon calling the non-const variant of In the particular case discussed here we would have something like
If |
@smowton Indeed, this assumes that the moved object isn't touched afterwards; in your example, I'd assume that 'something' isn't ever used after the move_to_operands. I would assume that to be the case across the code base, and would consider anything else to be a bug. |
Right, but operations against the destination of the move are allowed, and if they take place before the moved-from handle has died then a needless CoW break will occur. |
(in both my examples it's the target that's being altered, and the source handle just happens to still be alive) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 234ac62).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86463616
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of incrementally constructing objects do RAII.
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of incrementally constructing objects do RAII.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend using std::initializer_list
, and as previously mentioned, object to move
-> add
in general.
return_block.add(monitorexit); | ||
return_block.move_to_operands(code); | ||
code = return_block; | ||
code = code_blockt({monitorexit, code}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're going for this style, could I recommend replacing the constructor for code_blockt
that takes a std::list<const codet &>
with one taking a std::initializer_list
? The former means a bunch of heap allocates and deallocates, whereas the latter is basically a type-safe layer atop varargs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It could also take universal references, permitting {monitorexit, std::move(code)}
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Correction, it's not a wrapper on <stdarg>
style varargs as I'd thought, but it is a fixed-size array, probably stack-allocated, which still avoids much of the pointless back-and-forth of a temporary linked list)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(You could also use a variadic template... but that's probably more fuss than it's worth when the initializers are monovariant)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, there is unnecessary copying going on in the constructor. How about these:
explicit code_blockt(const std::vector<codet> &_statements):codet(ID_block)
{
operands()=(const std::vector<exprt> &)_statements;
}
explicit code_blockt(std::vector<codet> &&_statements):codet(ID_block)
{
operands()=std::move((std::vector<exprt> &&)_statements);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not std::initializer_list<codet>
? Should be just like vector, except without the heap alloc; plus it's (one of the) new standard ways to do varargs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that add anything over offering the vector interface? Note that the vector offers a constructor from std::initializer_list, and that the vector needs to exist eventually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think marginally, but the difference is small so let's go with this. It's definitely much better than std::list
for this purpose.
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of incrementally constructing objects do RAII.
234ac62
to
8c40aa1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 8c40aa1).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86610869
This is some drive-by cleanup resulting from reviewing diffblue#3067. Instead of incrementally constructing objects do RAII.
@smowton To make progress on this, how about a variant of code_blockt::add that gets an rvalue reference? You'd then write
in the scenario in which you would modify the added statement after adding. |
Sure? Don't mind re: |
8c40aa1
to
28beb76
Compare
Ok, done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 28beb76).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/86726683
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo clang-format https://travis-ci.org/diffblue/cbmc/jobs/436556422#L509
The new constructors avoid building a vector from a list. The vector is constructed directly at the call site of the constructor.
.move_operands() is avoided; there is no measurable performance benefit.
28beb76
to
46232f0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passed Diffblue compatibility checks (cbmc commit: 46232f0).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/87134284
This is the trivial part of #2830.